package com.jumper.cwolf.leetcode;

import cn.hutool.json.JSONUtil;

/**
 * 描述：
 *
 * @author cwolf
 * @version 1.0
 * @date 2021/4/2 11:18
 */
public class Code88 {

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int total = m+n;
        int[] numsNew = new int[total];
        int index1=0,index2 = 0;
        for(int i=0;i<total;i++){
            //结束了
            if(index1==m||index2==n){
                break;
            }
           int num1 = nums1[index1];
           int num2 = nums2[index2];
           if(num1>num2){
               numsNew[i] = num2;
               index2++;
           }else if(num1<num2){
               numsNew[i] = num1;
               index1++;
           }else{
               numsNew[i] = num1;
               numsNew[i+1] = num1;
               index1++;index2++;
               i++;
           }
        }
        if(index1!=m){
            int sy = m - index1;
            for(int i=0;i<sy;i++){
                nums1[total-i-1] = nums1[m-i-1];

            }
        }
        if(index2!=n){
            int sy = n - index2;
            for(int i=0;i<sy;i++){
                nums1[total-i-1] = nums2[n-i-1];
            }
        }

        for(int i=0;i<index1+index2;i++){
            nums1[i] = numsNew[i];
        }




        System.out.println(JSONUtil.toJsonStr(nums1));
    }




    public static void main(String[] args) {
        int[] arr = new int[]{
                2
        };
        int[] arr1 = new int[]{
               1
        };

        int[] arrNew = new int[arr.length+arr1.length];
        for(int i=0;i<arrNew.length;i++){
            arrNew[i] = i<arr.length?arr[i]:0;
        }
       new Code88().merge(arrNew,arr.length,arr1,arr1.length);
        System.out.println("");
    }

}
